package zhihui.mobile.presenter.shop;

import android.content.Context;

import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import zhihui.mobile.R;
import zhihui.mobile.bean.shop.order.ShopOrderDetail;
import zhihui.mobile.bean.wx.WeixinPayInfo;
import zhihui.mobile.interfaces.net.IHttpCallBackListener;
import zhihui.mobile.manager.pay.PayManager;
import zhihui.mobile.manager.shop.ShopOrderManager;
import zhihui.mobile.ui.IView;
import zhihui.mobile.ui.shop.order.IShopOrderDetailContract;

/**
 * Created by 张公铭 on 2017/9/11 0011.
 */

public class ShopOrderDetailPresenter implements IShopOrderDetailContract.IShopOrderDetailPresenter
{
    private Context mContext;
    private IShopOrderDetailContract.IShopOrderDetailView detailView;

    @Override
    public void init(IView view, Context context)
    {
        mContext = context;
        detailView = (IShopOrderDetailContract.IShopOrderDetailView) view;
    }

    @Override
    public void onResume()
    {

    }

    @Override
    public void onStart()
    {

    }

    @Override
    public void onPause()
    {

    }

    @Override
    public void onStop()
    {

    }

    @Override
    public void onDestroy()
    {

    }

    @Override
    public void initData()
    {
        ShopOrderDetail orderDetail = ShopOrderManager.getInstance().getCurOrderDetail();
        detailView.initViewData(orderDetail);
    }

    @Override
    public void alipay(String orderId, int paymoney)
    {
        if (orderId.equals("0") || paymoney <= 0)
        {
            detailView.showMessage("支付信息错误！");
            return;
        }
        detailView.showLoading();
        PayManager.getInstance().ilpay(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                detailView.hindLoading();
                try
                {
                    String data = result.getString("data");
                    detailView.alipay(data);
                } catch (JSONException e)
                {
                    e.printStackTrace();
                    detailView.showMessage(mContext.getString(R.string.json_error));
                }
            }

            @Override
            public void failed(String message)
            {
                detailView.hindLoading();
                detailView.showMessage(message);
            }
        }, ""+orderId, paymoney);
    }

    @Override
    public void weixinPay(String orderId, int paymoney)
    {
        if (orderId.equals("0") || paymoney <= 0)
        {
            detailView.showMessage("支付信息错误！");
            return;
        }

        detailView.showLoading();
        PayManager.getInstance().weixin(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                detailView.hindLoading();
                try
                {
                    Gson gson = new Gson();
                    WeixinPayInfo payInfo = gson.fromJson(result.getJSONObject("data").toString(), WeixinPayInfo.class);
                    detailView.weixinPay(payInfo);
                } catch (JSONException e)
                {
                    e.printStackTrace();
                    detailView.showMessage(mContext.getString(R.string.json_error));
                }
            }

            @Override
            public void failed(String message)
            {
                detailView.hindLoading();
                detailView.showMessage(message);
            }
        }, ""+orderId, paymoney);
    }

    @Override
    public void updateOrderStatus(String orderId, final int status)
    {
        detailView.showLoading();
        ShopOrderManager.getInstance().updateOrderStatus(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                detailView.hindLoading();
                String message = "成功！";
                switch (status)
                {
                    case 4:
                        message = "确认收货成功";
                        break;
                    case 5:
                        message = "取消订单成功";
                        break;
                    case 6:
                        message = "申请退货成功";
                        break;
                }
                detailView.showMessage(message);
                detailView.hindfooter();
            }

            @Override
            public void failed(String message)
            {
                detailView.hindLoading();
                detailView.showMessage(message);
            }
        }, orderId, status);
    }
}
